/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Object.BookProperties;

import Object.Database;
import Object.Page;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author PhongLK60473
 */
public class Type {
    private int typeId;
    private String typeName;
    private String typeDescription;

    public Type(int typeId, String typeName) {
        this.typeId = typeId;
        this.typeName = typeName;
        this.typeDescription="";
    }

    public String getTypeDescription() {
        return typeDescription;
    }

    public void setTypeDescription(String typeDescription) {
        this.typeDescription = typeDescription;
    }

    public Type(int typeId, String typeName, String typeDescription) {
        this.typeId = typeId;
        this.typeName = typeName;
        this.typeDescription = (typeDescription==null)?"":typeDescription;
    }

    public Type(int typeId) {
        this.typeId = typeId;
    }

    public Type(String typeName) {
        this.typeName = typeName;
    }
    
    public int getTypeId() {
        return typeId;
    }

    public Type() {
    }
    public static Type[] AllTypes(){
        ArrayList alTypes = new ArrayList();
        Database db = new Database();

        if(db.isOK){
            String strSQL;
            PreparedStatement stm=null;
            ResultSet rs = null;
            try {
                strSQL = "SELECT * FROM BookTypes";
                stm = db.con.prepareStatement(strSQL);
                rs = stm.executeQuery();

                while(rs.next()){
                    Type type = new Type(rs.getInt(1),rs.getString(2),rs.getString(3));
                    alTypes.add(type);
                }
            } catch (SQLException e) {
                System.out.println("Type(){ SQL : "+e.getMessage());
            } finally {
                try{
                    rs.close();
                    stm.close();
                    db.con.close();
                } catch (SQLException e){
                }
            }
        }else{
            System.out.println("Type(){ Connect database failed }");
        }

        Type[] types = new Type[alTypes.size()];
        alTypes.toArray(types);
        return types;
    }
    public void setTypeId(int typeId) {
        this.typeId = typeId;
    }

    public String getTypeName() {
        return typeName;
    }

    public void setTypeName(String typeName) {
        this.typeName = typeName;
    }

    public static boolean AddType(HttpServletRequest request){
        boolean rt=false;
        try{
            String name = request.getParameter("txtName").toString()+"";

            Type type = new Type(name);
            rt=type.Add();
        }catch(Exception e){
            rt=false;
            e.printStackTrace();
        }
        if(!rt){
            Page.setMessage("Add Type failed!", request);
        }else{
            Page.setMessage("Add Type successfull!", request);
        }
        return rt;
    }
    public boolean Add(){
        boolean rt=false;
        Database db = new Database();
        if(db.isOK){
            String strSQL;
            PreparedStatement stm=null;
            int rs;
            try {
                strSQL = "INSERT INTO BookTypes (name)"
                        + " VALUES(?)";
                stm = db.con.prepareStatement(strSQL);
                stm.setString(1, this.typeName);

                rs = stm.executeUpdate();

                if(rs>0){
                    rt=true;
                }else{
                    rt=false;
                }
            } catch (SQLException e) {
                rt=false;
            } finally {
                try{
                    stm.close();
                    db.con.close();
                } catch (SQLException e){

                }
            }
        }else{
            rt=false;
        }
        return rt;
    }
    public static boolean SaveType(HttpServletRequest request){
        boolean rt=false;
        try{
            String name = request.getParameter("txtName").toString()+"";
            int typeid = Integer.parseInt(request.getParameter("typeid"));
            Type type = new Type(typeid, name);
            rt=type.Save();
        }catch(Exception e){
            rt=false;
            e.printStackTrace();
        }
        if(!rt){
            Page.setMessage("Edit Type failed!", request);
        }else{
            Page.setMessage("Edit Type successfull!", request);
        }
        return rt;
    }
    public boolean Save(){
        boolean rt=false;
        Database db = new Database();
        if(db.isOK){
            String strSQL;
            PreparedStatement stm=null;
            int rs;
            try {
                strSQL = "UPDATE BookTypes SET name = ? "
                        + "WHERE id = ?";
                stm = db.con.prepareStatement(strSQL);
                stm.setString(1, this.typeName);
                stm.setInt(2, this.typeId);

                rs = stm.executeUpdate();

                if(rs>0){
                    rt=true;
                }else{
                    rt=false;
                }
            } catch (SQLException e) {
                rt=false;
            } finally {
                try{
                    stm.close();
                    db.con.close();
                } catch (SQLException e){

                }
            }
        }else{
            rt=false;
        }
        return rt;
    }
    public static boolean DeleteType(HttpServletRequest request){
        boolean rt=false;
        try{
            int typeid = Integer.parseInt(request.getParameter("typeid"));
            Type type = new Type(typeid);
            rt=type.Delete();
        }catch(Exception e){
            rt=false;
            e.printStackTrace();
        }
        if(!rt){
            Page.setMessage("Delete Type failed!", request);
        }else{
            Page.setMessage("Delete Type successfull!", request);
        }
        return rt;
    }
    public boolean Delete(){
        boolean rt=false;
        Database db = new Database();
        if(db.isOK){
            String strSQL;
            PreparedStatement stm=null;
            int rs;
            try {
                strSQL = "DELETE BookTypes WHERE id = ?";
                stm = db.con.prepareStatement(strSQL);
                stm.setInt(1, this.typeId);

                rs = stm.executeUpdate();

                if(rs>0){
                    rt=true;
                }else{
                    rt=false;
                }
            } catch (SQLException e) {
                rt=false;
            } finally {
                try{
                    stm.close();
                    db.con.close();
                } catch (SQLException e){

                }
            }
        }else{
            rt=false;
        }
        return rt;
    }
}
